﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BussinessEntity;
using BussinessLayer;
using Utilities;
using Configurations;

namespace PresentationLayer
{
    public partial class frmReceiptDetail : DevComponents.DotNetBar.OfficeForm
    {
        public List<ReceiptDetail> ListCT { get; set; }
        public CapNhatDS CapNhat { get; set; }

        private List<Service> ListDV;

        public frmReceiptDetail(List<ReceiptDetail> ListCT, CapNhatDS cn)
        {
            InitializeComponent();
            this.ListCT = ListCT;
            CapNhat = cn;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        void LoadForm()
        {
            var rs = ServiceManager.GetAll();
            if (rs.Array.Count == 0)
            {
                MessageBox.Show("Chưa có thông tin dịch vụ nào", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                Close();
            }
            ListDV = rs.Array;
            cbServiceName.DataSource = cbServiceID.DataSource = ListDV;
            cbServiceName.DisplayMember = "ServiceName";
            cbServiceName.ValueMember = cbServiceID.DisplayMember = cbServiceID.ValueMember = "ServiceID";
            foreach (var item in ListDV)
            {

                cbServiceID.AutoCompleteCustomSource.Add(item.ServiceID);
                cbServiceName.AutoCompleteCustomSource.Add(item.ServiceName);
            }
            cbServiceName.AutoCompleteMode = cbServiceID.AutoCompleteMode = AutoCompleteMode.SuggestAppend;
            cbServiceName.AutoCompleteSource = cbServiceID.AutoCompleteSource = AutoCompleteSource.CustomSource;

            
        }

        private void frmBillDetail_Load(object sender, EventArgs e)
        {
            LoadForm();
        }

        private void cbServiceName_SelectedValueChanged(object sender, EventArgs e)
        {
            try
            {
                var dv = ListDV.Find(s => s.ServiceID == DataUtil.ToString(cbServiceName.SelectedValue));
                if (dv != null)
                {
                    txtUnitPrice.Text = dv.UnitPriceVND;
                    nudQuantity.Maximum = dv.Quantity;
                    if (dv.ServiceCategory != ServiceState.ThayPhanCung)
                        nudQuantity.Maximum = 1000;
                    var ct = ListCT.Find(s => s.ServiceID == DataUtil.ToString(cbServiceID.SelectedValue));
                    if (ct != null)
                    {
                        var ctt = ReceiptDetailManager.GetSingle(ct.ReceiptID, ct.ServiceID);
                        if (ctt.Success)
                            nudQuantity.Maximum += ctt.Object.Quantity;
                        if (dv.ServiceCategory != ServiceState.ThayPhanCung)
                            nudQuantity.Maximum = 1000;
                        nudQuantity.Value = ct.Quantity;
                        txtUnitPrice.Text = ct.UnitPriceVND;
                    }
                    if (dv.ServiceCategory != ServiceState.ThayPhanCung)
                        nudQuantity.Maximum = 1000;
                }
            }
            catch (Exception)
            {
            }
        }

        ReceiptDetail GetInformation()
        {
            ReceiptDetail ct = new ReceiptDetail();
            ct.Service = ServiceManager.GetSingle(DataUtil.ToString(cbServiceID.SelectedValue)).Object;
            ct.UnitPrice = DataUtil.ChangeStringToMoney(txtUnitPrice.Text);
            ct.Quantity = DataUtil.ToInt(nudQuantity.Value);
            return ct;
        }

        bool CheckValidated()
        {
            if (DataUtil.ChangeStringToMoney(txtUnitPrice.Text) == 0)
            {
                txtUnitPrice.Focus();
                return false;
            }
            if (nudQuantity.Value == 0)
            {
                nudQuantity.Focus();
                return false;
            }
            return true;
        }

        private void btnSubmit_Click(object sender, EventArgs e)
        {
            if (CheckValidated())
            {
                var ct = GetInformation();
                var temp = ListCT.Find(s => s.ServiceID == ct.ServiceID);
                if (temp != null)
                {
                    temp.Quantity = ct.Quantity;
                    temp.UnitPrice = ct.UnitPrice;
                }
                else
                    ListCT.Add(ct);
                CapNhat(ListCT);
            }
            else
                MessageBox.Show("Nhập thông tin không hợp lệ", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Warning);
        }

        private void txtUnitPrice_Enter(object sender, EventArgs e)
        {
            txtUnitPrice.Text = DataUtil.ChangeStringToMoney(txtUnitPrice.Text).ToString();
        }

        private void txtUnitPrice_Leave(object sender, EventArgs e)
        {
            txtUnitPrice.Text = DataUtil.ChangeMoneyToString(txtUnitPrice.Text);
        }
    }
}